package com.yl.project.lvyou.untitled.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.yl.framework.config.YlConfig;
import com.yl.project.system.files.domain.UploadedFiles;
import com.yl.project.system.files.service.IUploadedFilesService;
import org.apache.commons.io.FileUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.yl.framework.aspectj.lang.annotation.Log;
import com.yl.framework.aspectj.lang.enums.BusinessType;
import com.yl.project.lvyou.untitled.domain.Untitled;
import com.yl.project.lvyou.untitled.service.IUntitledService;
import com.yl.framework.web.controller.BaseController;
import com.yl.framework.web.domain.AjaxResult;
import com.yl.common.utils.poi.ExcelUtil;
import com.yl.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 旅游景区信息管理Controller
 * 
 * @author wjs
 * @date 2025-03-10
 */
@Controller
@RequestMapping("/lvyou/untitled")
public class UntitledController extends BaseController
{
    private String prefix = "scenemanager/untitled";

    @Autowired
    private IUntitledService untitledService;

    @Autowired
    private IUploadedFilesService uploadedFilesService;

    @RequiresPermissions("scenemanager:untitled:view")
    @GetMapping()
    public String untitled()
    {
        return prefix + "/untitled";
    }

    /**
     * 查询旅游景区信息管理列表
     */
    @RequiresPermissions("scenemanager:untitled:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Untitled untitled)
    {
        startPage();
        List<Untitled> list = untitledService.selectUntitledList(untitled);
        return getDataTable(list);
    }

    /**
     * 导出旅游景区信息管理列表
     */
    @RequiresPermissions("scenemanager:untitled:export")
    @Log(title = "旅游景区信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Untitled untitled)
    {
        List<Untitled> list = untitledService.selectUntitledList(untitled);
        ExcelUtil<Untitled> util = new ExcelUtil<Untitled>(Untitled.class);
        return util.exportExcel(list, "untitled");
    }

    /**
     * 新增旅游景区信息管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存旅游景区信息管理
     */
    @RequiresPermissions("scenemanager:untitled:add")
    @Log(title = "旅游景区信息管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Untitled untitled)
    {
        untitled.setUpdateBy(getUserId().toString());
        return toAjax(untitledService.insertUntitled(untitled));
    }

    /**
     * 修改旅游景区信息管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        Untitled untitled = untitledService.selectUntitledById(id);
        mmap.put("untitled", untitled);
        return prefix + "/edit";
    }

    /**
     * 修改保存旅游景区信息管理
     */
    @RequiresPermissions("scenemanager:untitled:edit")
    @Log(title = "旅游景区信息管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Untitled untitled)
    {
        untitled.setUpdateBy(getUserId().toString());
        return toAjax(untitledService.updateUntitled(untitled));
    }

    /**
     * 删除旅游景区信息管理
     */
    @RequiresPermissions("scenemanager:untitled:remove")
    @Log(title = "旅游景区信息管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(untitledService.deleteUntitledByIds(ids));
    }

    /**
     * 跳转到上传页面页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/uploadImg/{id}")
    public String uploadImg(@PathVariable("id")Long id,ModelMap model){
        model.put("id", id);
        return prefix+"/uploadImg";
    }
    @GetMapping("/lookJingqutupian/{id}")
    public String lookJingqutupian(@PathVariable("id")Long id,ModelMap model){
        model.put("id", id);
        return prefix+"/lookJingqu";
    }
    @PostMapping("/lookTupianlist")
    @ResponseBody
    public TableDataInfo lookTupianlist(Long id){
       UploadedFiles uploadedFiles= new UploadedFiles();
        uploadedFiles.setYewuId(id);
        uploadedFiles.setYewuName("UNTITLED");
        startPage();
        List<UploadedFiles> list = uploadedFilesService.select(uploadedFiles);
        return getDataTable(list);
    }


    @PostMapping("/uploadJingquPic")
    @ResponseBody
    public AjaxResult tianjiajiaocai(HttpServletRequest request, HttpServletResponse response,
                                     @RequestParam(value = "id", required = true) Long id,
                                     @RequestParam(value = "imgIndex", required = true) Integer imgIndex,
                                     MultipartFile myFile){
        MultipartFile file = myFile;
        Map<String,Object> map = new HashMap<>();
        map.put("id", id);
        UploadedFiles files = new UploadedFiles();
        files.setCreateBy(getUserId().toString());;
        files.setYewuId(id);

        int res = 0;
        int count = uploadedFilesService.selectCount(files)+1;

        if (!file.isEmpty()) {
            try {
                String uploadFilePath = file.getOriginalFilename();
                // 截取上传文件的文件名
                String uploadFileName = uploadFilePath.substring(uploadFilePath.lastIndexOf('\\') + 1,
                        uploadFilePath.indexOf('.'));

                // 截取上传文件的后缀
                String uploadFileSuffix = uploadFilePath.substring(uploadFilePath.lastIndexOf("."));

                String fileName = UUID.randomUUID().toString();


                String path = YlConfig.getUploadPath();
                File file1 = new File(path);
                long size = FileUtils.sizeOf(file1);
                if (!file1.exists()) {
                    file1.mkdirs();
                }
                file.transferTo(new File(path + fileName + uploadFileSuffix));
                files.setFileName(uploadFileName+uploadFileSuffix);
                files.setFilePath(fileName + uploadFileSuffix);
                files.setFileType(uploadFileSuffix);
                files.setFileSize(size);
                files.setYewuName("UNTITLED");
                files.setXuhao(count+imgIndex);
                res= uploadedFilesService.insertUploadedFiles(files);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            return toAjax(false);
        }
        if(res >0){
            return toAjax(true);
        }else{
            return toAjax(false);
        }
    }
}
